package cn.bx.bid.ctrl;

import cn.bx.bid.entity.Page;
import cn.bx.bid.entity.Professor;
import cn.bx.bid.entity.User;
import cn.bx.bid.entity.WorkHistory;
import cn.bx.bid.service.ProfessorService;
import cn.bx.bid.util.DocUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class ProfessorController {
    @Autowired
    private ProfessorService professorService;

    //专家分页
    @RequestMapping(value = "professorsearch", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Page<Professor> paging(@RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "10") int limit, @RequestParam(value = "name", required = false) String name, @RequestParam(value = "tele", required = false) String tele, @RequestParam(value = "idcard", required = false) String idcard) {
        Page<Professor> paging = this.professorService.paging(page, limit, name, tele, idcard);
        return paging;
    }

    //新增专家
    @RequestMapping(value = "professoradd", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String add(@ModelAttribute("p") Professor p, HttpSession s) {
        User user = (User) s.getAttribute("USER_LOGIN");
        p.setCreateby(user.getUserName());
        int r = this.professorService.add(p);
        return "{\"msg\":" + (r > 0) + "}";
    }

    //修改专家
    @RequestMapping(value = "professorupdate", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String update(@ModelAttribute("p") Professor p, HttpSession s) {
        User user = (User) s.getAttribute("USER_LOGIN");
        p.setCreateby(user.getUserName());
        int r = this.professorService.update(p);
        return "{\"msg\":" + (r > 0) + "}";
    }

    //更新专家获取值
    @RequestMapping(value = "professorget-{id}", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Professor get(@PathVariable("id") long id) {
        Professor professor = this.professorService.get(id);
        return professor;
    }

    //批量删除和单个删除
    @RequestMapping(value = "professorbatchdel", produces = "application/json;charset=utf-8")
    @ResponseBody
    public int batchdel(@RequestParam(value = "ids[]") long[] ids) {
        int r = this.professorService.batchDel(ids);
        return r;
    }

    //新增工作记录
    @RequestMapping(value = "historyadd", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String historyadd(@ModelAttribute("w") WorkHistory w) {
        int r = professorService.addHistory(w);
        return "{\"msg\":" + (r > 0) + "}";
    }

    //更新工作记录
    @RequestMapping(value = "historyupdate", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String historyupdate(@ModelAttribute("w") WorkHistory w) {
        int r = professorService.updateHistory(w);
        return "{\"msg\":" + (r > 0) + "}";
    }

    //删除工作记录
    @RequestMapping(value = "historydel", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String historyupdate(@RequestParam("id") long id, @RequestParam("hisid") long hisid) {
        int r = professorService.delHis(id, hisid);
        return "{\"msg\":" + (r > 0) + "}";
    }

    //更新工作记录获取值
    @RequestMapping(value = "getonehistory", produces = "application/json;charset=utf-8")
    @ResponseBody
    public WorkHistory getOneHistory(@RequestParam("id") long id) {
        WorkHistory history = this.professorService.getOneHistory(id);
        return history;
    }

    //下载word
    @RequestMapping(value = "exportToWord", produces = "application/json;charset=utf-8")
    @ResponseBody
    public void exportToWord(@RequestParam("id") long id, HttpServletResponse response) throws Exception {
        Professor p = this.professorService.get(id);
        java.util.Map<String, Object> map = new java.util.HashMap<String, Object>();
        map.put("p", p);
        String path = Math.random() + ".doc";
        DocUtil.exportProfessor(path, map);
        File file = new File(path);
        response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(p.getProfessorname() + ".doc", "UTF-8"));
        FileInputStream fin = new FileInputStream(file);
        byte[] b = new byte[fin.available()];
        fin.read(b);
        fin.close();
        ServletOutputStream out = response.getOutputStream();
        out.write(b);
        out.flush();
        file.delete();//下载后删除文件
    }

    @InitBinder
    public void init(WebDataBinder binder) {
        CustomDateEditor dateEditor = new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true);
        binder.registerCustomEditor(Date.class, dateEditor);
    }
}
